//
//  LayoutCommentsTableViewCell.m
//  UniversalTableView
//
//  Created by 冷悦 on 16/3/15.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "LayoutCommentsTableViewCell.h"
#import "StarsControlView.h"
#import "Masonry.h"
#import "LayoutCommentsTableViewCellModel.h"
#import "UIImageView+FadeWebCache.h"

#ifndef kLayoutCommentsTableViewMargin
#define kLayoutCommentsTableViewMargin 10.0f
#endif

@interface LayoutCommentsTableViewCell ()

@property (nonatomic, strong)UIImageView *avatarImageView;

@property (nonatomic, strong)UILabel *nameLabel;

@property (nonatomic, strong)StarsControlView *startView;

@property (nonatomic, strong)UILabel *dateLabel;

@property (nonatomic, strong)UILabel *commentLabel;

@property (nonatomic, strong)UIButton *positiveButton;

@property (nonatomic, strong)UILabel *positiveCountLabel;

@property (nonatomic, strong)UIButton *negtiveButton;

@property (nonatomic, strong)UILabel *negtiveCountLabel;

@end

@implementation LayoutCommentsTableViewCell


#pragma mark - Initialize
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupConstraints];
        self.backgroundColor = BackgroundColor240;
    }
    
    return self;
}

#pragma mark - Private
- (void)setupConstraints{
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(FifteenoffsetViewMargin);
        make.top.equalTo(self.contentView).offset(TenOffsetViewMargin);
        make.width.equalTo(self.contentView).multipliedBy(0.13);
        make.height.equalTo(self.avatarImageView.mas_width);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarImageView);
        make.left.equalTo(self.avatarImageView.mas_right).offset(kLayoutCommentsTableViewMargin);
        make.height.lessThanOrEqualTo(@25);
    }];
    
    [self.startView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel);
        make.height.equalTo(ScreenWidth*0.05);
        make.width.mas_equalTo(ScreenWidth*0.25);
        make.left.equalTo(self.nameLabel.mas_right).offset(FiveOffsetViewMargin);
        make.right.lessThanOrEqualTo(self.contentView);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.negtiveButton);
        make.right.equalTo(self.contentView).offset(-FifteenoffsetViewMargin);
        make.left.mas_greaterThanOrEqualTo(self.negtiveCountLabel.mas_right).offset(TenOffsetViewMargin);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(TenOffsetViewMargin);
        make.left.equalTo(self.nameLabel);
        make.right.equalTo(self.contentView).offset(-FifteenoffsetViewMargin);
    }];
    [self.positiveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.commentLabel.mas_bottom).offset(TenOffsetViewMargin);
        make.left.equalTo(self.commentLabel);
        make.width.equalTo(self.positiveButton.mas_height);
        make.height.equalTo(@20);
        make.bottom.equalTo(self.contentView).offset(-FifteenoffsetViewMargin);
    }];
    
    [self.positiveCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.positiveButton);
        make.left.equalTo(self.positiveButton.mas_right).offset(FiveOffsetViewMargin);
    }];
    
    [self.negtiveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.top.bottom.equalTo(self.positiveButton);
        make.left.equalTo(self.positiveCountLabel.mas_right).offset(TweentyOffsetViewMargin*2);
    }];
    
    [self.negtiveCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.top.bottom.equalTo(self.positiveCountLabel);
        make.left.equalTo(self.negtiveButton.mas_right).offset(kLayoutCommentsTableViewMargin/2);
    }];
}

#pragma mark - Getters
- (UIImageView *)avatarImageView{
    if (_avatarImageView == nil) {
        _avatarImageView = [UIImageView new];
        [self.contentView addSubview:_avatarImageView];
    }
    
    return _avatarImageView;
}

- (UILabel *)nameLabel{
    if (_nameLabel == nil) {
        _nameLabel = [UILabel new];
        _nameLabel.font = TextFont18;
        _nameLabel.textColor = TextColor120;
        [self.contentView addSubview:_nameLabel];
    }
    
    return _nameLabel;
}

- (StarsControlView *)startView{
    if (_startView == nil) {
        _startView = [StarsControlView new];
        _startView.backgroundColor = [UIColor clearColor];
        _startView.userInteractionEnabled = NO;
        _startView.starCount = 5;
        _startView.starLineCount = 1;
        _startView.starsLevel = 3;
        [self.contentView addSubview:_startView];
    }
    
    return _startView;
}

- (UILabel *)commentLabel{
    if (_commentLabel == nil) {
        _commentLabel = [UILabel new];
        _commentLabel.font = TextFont14;
        _commentLabel.numberOfLines = 0;
        _commentLabel.textColor =  TextColor120;
        [self.contentView addSubview:_commentLabel];
    }
    
    return _commentLabel;
}

- (UILabel *)dateLabel{
    if (_dateLabel == nil) {
        _dateLabel = [UILabel new];
        _dateLabel.font = TextFont14;
        _dateLabel.textAlignment = NSTextAlignmentRight;
        _dateLabel.textColor = TextColor150;
        [self.contentView addSubview:_dateLabel];
    }
    
    return _dateLabel;
}

- (UIButton *)positiveButton{
    if (_positiveButton == nil) {
        _positiveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_positiveButton setIconWithKey:@"layoutPositiveIcon" withPlaceHolderString:DefaultNetIcon1To1 forState:UIControlStateNormal];
        [self.contentView addSubview:_positiveButton];
    }
    
    return _positiveButton;
}

- (UILabel *)positiveCountLabel{
    if (_positiveCountLabel == nil) {
        _positiveCountLabel = [UILabel new];
        _positiveCountLabel.font = TextFont14;
        _positiveCountLabel.textColor = TextColor120;
        [self.contentView addSubview:_positiveCountLabel];
    }
    
    return _positiveCountLabel;
}

- (UIButton *)negtiveButton{
    if (_negtiveButton == nil) {
        _negtiveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_negtiveButton setIconWithKey:@"layoutNegtiveIcon" withPlaceHolderString:DefaultNetIcon1To1 forState:UIControlStateNormal];
        [self.contentView addSubview:_negtiveButton];
    }
    
    return _negtiveButton;
}

- (UILabel *)negtiveCountLabel{
    if (_negtiveCountLabel == nil) {
        _negtiveCountLabel = [UILabel new];
        _negtiveCountLabel.font = TextFont14;
        _negtiveCountLabel.textColor = TextColor120;
        [self.contentView addSubview:_negtiveCountLabel];
    }
    
    return _negtiveCountLabel;
}

#pragma mark - Setters
- (void)setModel:(LayoutCommentsTableViewCellModel *)model{
    [self.avatarImageView setRoundImage:model.avatarURL PlaceHolderImage:DefaultNetPicture1To1 borderWidth:0 borderColor:[UIColor clearColor] andRadius:0.0];
    self.nameLabel.text = model.name;
    self.startView.starsLevel = model.startCount;
    self.dateLabel.text = model.date;
    self.commentLabel.text = model.comment;
    self.positiveCountLabel.text = [NSString stringWithFormat:@"(%ld)", (long)model.positiveCount];
    self.negtiveCountLabel.text = [NSString stringWithFormat:@"(%ld)", (long)model.negativeCount];
    
}
@end
